﻿using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using Microsoft.CSharp;

namespace Sharp.Infrastructure
{
    /// <summary>
    /// 取得服务端信息
    /// </summary>
    [DebuggerStepThrough]
    public class WebServer
    {
        #region 穿过代理服务器取远程用户真实IP地址
        /// <summary>
        /// 穿过代理服务器取远程用户真实IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            return GetIP(HttpContext.Current);
        }
        /// <summary>
        /// 过代理服务器取远程用户真实IP地址
        /// </summary>
        /// <param name="current"></param>
        /// <returns></returns>
        public static string GetIP(HttpContext current)
        {
            try
            {
                string ip = current.Request.ServerVariables["HTTP_VIA"] != null ? current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : current.Request.ServerVariables["REMOTE_ADDR"];
                ip = ip.Replace("::1", "127.0.0.1");

                return ip;
            }
            catch
            {
                return "0.0.0.0";
            }
        }
        #endregion

    }
}